﻿<%@ WebHandler Language="C#" Class="GetMap" %>

using System;
using System.Web;

public class GetMap : IHttpHandler 
{   
    public void ProcessRequest (HttpContext context) 
    {
        MapHandler oMapHandler = new MapHandler();
        string sSVGContent = "";
        
        string sIsGetFullExtentMap = context.Request.QueryString["fullextent"];
        string sMinLongitude = context.Request.QueryString["minlon"];
        string sMinLatitude = context.Request.QueryString["minlat"];
        string sMaxLongitude = context.Request.QueryString["maxlon"];
        string sMaxLatitude = context.Request.QueryString["maxlat"];
        string sScale = context.Request.QueryString["scale"];

        double dMinLongitude = 0;
        double dMinLatitude = 0;
        double dMaxLongitude = 0;
        double dMaxLatitude = 0;
        double dScale = 0;

        try
        {
            dScale = double.Parse(sScale);
            dMinLongitude = double.Parse(sMinLongitude);
            dMinLatitude = double.Parse(sMaxLatitude) * -1;
            dMaxLongitude = double.Parse(sMaxLongitude);
            dMaxLatitude = double.Parse(sMinLatitude) * -1;

            this.InitMapLevelInfos();
            
            foreach (MapLevelInfomation oMapLevelInfo in GlobeVaribles.MapLevelInfos)
            {
                if (dScale > oMapLevelInfo.MaxScale && dScale <= oMapLevelInfo.MinScale)
                {
                    sSVGContent = oMapHandler.GetSVGMapData(dMinLongitude, dMinLatitude, dMaxLongitude, dMaxLatitude, oMapLevelInfo);
                    break;
                }
            }

            context.Response.Write(sSVGContent);
        }
        catch (Exception ex)
        {
            context.Response.Write(ex.StackTrace);
        }
    }
 
    public bool IsReusable 
    {
        get {return false;}
    }    
        
    private void InitMapLevelInfos()
    {   
        MapLevelInfomation oLevel1 = new MapLevelInfomation();
        MapLevelInfomation oLevel2 = new MapLevelInfomation();
        MapLevelInfomation oLevel3 = new MapLevelInfomation();

        oLevel1.LayerInfomations.Add(new LayerInformation("states", "the_geom", "state_name", GeometryType.Polygon));
        oLevel1.Level = 1;
        oLevel1.MinScale = 1;
        oLevel1.MaxScale = 2 / 3;

        oLevel2.LayerInfomations.Add(new LayerInformation("counties", "the_geom", "name", GeometryType.Polygon));
        oLevel2.LayerInfomations.Add(new LayerInformation("us_rivers", "the_geom", "name", GeometryType.PolyLine));
        oLevel2.LayerInfomations.Add(new LayerInformation("us_lakes", "the_geom", "name", GeometryType.Polygon));
        oLevel2.Level = 1;
        oLevel2.MinScale = 2 / 3;
        oLevel2.MaxScale = 1 / 3;

        oLevel3.LayerInfomations.Add(new LayerInformation("counties", "the_geom", "name", GeometryType.Polygon));
        oLevel3.LayerInfomations.Add(new LayerInformation("us_rivers", "the_geom", "name", GeometryType.PolyLine));
        oLevel3.LayerInfomations.Add(new LayerInformation("us_lakes", "the_geom", "name", GeometryType.Polygon));
        oLevel3.LayerInfomations.Add(new LayerInformation("cities", "the_geom", "name", GeometryType.Point));
        oLevel3.Level = 1;
        oLevel3.MinScale = 1 / 3;
        oLevel3.MaxScale = 0;

        GlobeVaribles.MapLevelInfos.Clear();
        GlobeVaribles.MapLevelInfos.Add(oLevel1);
        GlobeVaribles.MapLevelInfos.Add(oLevel2);
        GlobeVaribles.MapLevelInfos.Add(oLevel3);
        
    }
}

